﻿using Common.Device.DeviceDb;
using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Common.Device.Models
{
    internal class ZhiKong : Device
    {
        //MesDevice Mesdb = null;
        private MesDevice Mesdb = new MesDevice();
        /// <summary>
        /// 测试模式
        /// </summary>
        public int 测试项序号 { get; set; }
        public string 函数编号 { get; set; }
        public ZhiKongEnum.TestMode mode { get; set; }
        public int endtime { get; set; }
        public int 通道 { get; set; }
        public uint setcmd { get; set; }
        public uint setops { get; set; }
        public ulong setdata { get; set; }
        public uint setreserve1 { get; set; }
        public uint setreserve2 { get; set; }
        public uint setreserve3 { get; set; }
        public uint getcmd { get; set; }
        public uint getops { get; set; }
        public ulong getdata { get; set; }
        public uint getreserve1 { get; set; }
        public uint getreserve2 { get; set; }
        public uint getreserve3 { get; set; }
        public override Order GetOrder(string deviceName)
        {
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            switch (mode)
            {
                case ZhiKongEnum.TestMode.开机确认:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata , setreserve1 , setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3 });
                    break;
                case ZhiKongEnum.TestMode.自动测试:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3 });
                    break;
                case ZhiKongEnum.TestMode.手动测试:
                    order.range = GetRange("mV");
                    order.range1 = GetRange1("C");
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3 });
                    break;
                case ZhiKongEnum.TestMode.手动确认:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3 });
                    break;
                case ZhiKongEnum.TestMode.获取蓝牙:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3 });
                    break;
                case ZhiKongEnum.TestMode.获取CPU:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3 });
                    break;
                case ZhiKongEnum.TestMode.写入蓝牙:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3 , 测试项序号 , 函数编号 });
                    break;
                case ZhiKongEnum.TestMode.初次测试检查:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3, 测试项序号, 函数编号 });
                    break;
                case ZhiKongEnum.TestMode.复测检查:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3, 测试项序号, 函数编号 });
                    break;
                case ZhiKongEnum.TestMode.初次通过上传:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3, 测试项序号, 函数编号 });
                    break;
                case ZhiKongEnum.TestMode.复测上传:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3, 测试项序号, 函数编号 });
                    break;
                case ZhiKongEnum.TestMode.模式切换确认:
                    order.deviceModel = JsonConvert.SerializeObject(new { 通道, setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, getcmd, getops, getdata, getreserve1, getreserve2, getreserve3, 测试项序号, 函数编号,endtime });
                    break;
            }
            return order;
        }

        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {

            var device = dev as IZhiKong;
            var result = new DeviceResult();
            var time = DateTime.Now;
            ulong btnumber;
            string sntext = "";
            string sn = "";
            string code = "";
            string btmac = "";
            string btname = "";
            string rev = "";
            string number = "";
            switch (mode)
            {
                case ZhiKongEnum.TestMode.开机确认:
                    //初始化
                    var r1 = device.ReadSensor0(setcmd, setops, setdata, setreserve1, setreserve2, setreserve3,通道);
                    //初始化值
                    result.success = r1.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r1.Err;
                        break;
                    }
                    string res1 = $"cmd:{r1.Value[0]},ops:{r1.Value[1]},date:{r1.Value[2]}";

                    if (r1.Value[0]!=getcmd.ToString())
                    {
                        result.success=false;
                        result.result = res1+"cmd异常";
                        break;
                    }
                    if (r1.Value[1]!=getops.ToString())
                    {
                        result.success=false;
                        result.result = res1+"ops异常";
                        break;
                    }
                    if (r1.Value[2]!=getdata.ToString())
                    {
                        result.success=false;
                        result.result = res1+"date异常";
                        break;
                    }
                    result.result = "OK";
                    break;
                case ZhiKongEnum.TestMode.自动测试:
                    //开始测试模式
                    var r2 = device.ReadSensor1(setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, 通道);
                    //初始化值
                    result.success = r2.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r2.Err;
                        break;
                    }
                    string res = $"cmd:{r2.Value[0]},ops:{r2.Value[1]},date:{r2.Value[2]}";

                    if (r2.Value[0]!=getcmd.ToString())
                    {
                        result.success=false;
                        result.result = res+"cmd异常";
                        break;
                    }
                    if (r2.Value[1]!=getops.ToString())
                    {
                        result.success=false;
                        result.result = res+"ops异常";
                        break;
                    }
                    if (r2.Value[2]!=getdata.ToString())
                    {
                        result.success=false;
                        result.result = res+"date异常";
                        break;
                    }
                    break;
                case ZhiKongEnum.TestMode.手动测试:
                    //开始测试模式
                    var r3 = device.ReadSensor2(setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, 通道);
                    //初始化值
                    result.success = r3.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r3.Err;
                        break;
                    }
                    break;
                case ZhiKongEnum.TestMode.手动确认:
                    var r4 = device.ReadSensor3(setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, 通道);
                    //初始化值
                    result.success = r4.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r4.Err;
                        break;
                    }
                    break;
                case ZhiKongEnum.TestMode.获取蓝牙:
                    
                    var r5 = device.ReadSensor4(setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, 通道);
                    //初始化值
                    result.success = r5.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r5.Err;
                        break;
                    }
                    
                    string res5 = $"cmd:{r5.Value[0]},ops:{r5.Value[1]},date:{r5.Value[2]}";


                    if (r5.Value[0]!=setcmd.ToString())
                    {
                        result.success=false;
                        result.result = res5+"cmd异常";
                        break;
                    }
                    if (r5.Value[1]!=setops.ToString())
                    {
                        result.success=false;
                        result.result = res5+"ops异常";
                        break;
                    }
                    result.result = r5.Value[2];
                    //result.value = getdata.ToString("X2");
                    //if (r5.Value[2]!=getdata.ToString("X2"))
                    //{
                    //    result.success=false;
                    //    result.result = res5+"date异常";
                    //    break;
                    //}
                    break;
                case ZhiKongEnum.TestMode.获取CPU:
                    var r6 = device.ReadSensor4(setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, 通道);
                    //初始化值
                    result.success = r6.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r6.Err;
                        break;
                    }
                    string res6 = $"cmd:{r6.Value[0]},ops:{r6.Value[1]},date:{r6.Value[2]}";


                    if (r6.Value[0]!=setcmd.ToString())
                    {
                        result.success=false;
                        result.result = res6+"cmd异常";
                        break;
                    }
                    if (r6.Value[1]!=setops.ToString())
                    {
                        result.success=false;
                        result.result = res6+"ops异常";
                        break;
                    }
                    result.result = getdata.ToString("X2");
                    break;
                case ZhiKongEnum.TestMode.写入蓝牙:

                    //var time = DateTime.Now;
                    //ulong btnumber ;
                    //string sntext = "";
                    //string sn = "";
                    //string code = "";
                    //string btmac = "";
                    //string btname = "";
                    //string rev = "";
                    //string number = "";
                    if (testOrder.TestOrders.Count >= 测试项序号)
                    {
                        //获取对应测试项测试结果
                        var Parameter = testOrder.TestOrders[测试项序号 - 1]?.orders.FirstOrDefault(it => it.no == 函数编号.ToString());
                        if (Parameter!=null)
                        {
                            sntext = Parameter.result.result;
                        }
                    }
                    else
                    {
                        result.success = false;
                        result.result = "缺失取值数据" + 测试项序号 + 函数编号;
                    }
                    result.result = sntext+";";
                    string[] parts = sntext.Split(';');
                    if (parts.Length==3)
                    {
                        sn = parts[0];
                        rev = parts[1];
                        btname = parts[2];
                    }
                    else
                    {
                        sn = parts[4];
                        rev = parts[3];
                        btname = parts[1];
                    }
                    code = sn.Substring(0,14);
                    number = btname.Substring(11);

                    btnumber = ulong.Parse(number);
                    //ZhiKongInfo one = new ZhiKongInfo() {

                    //    SN = sn,
                    //    Code = code,
                    //    BT_NAME = btname,
                    //    TestOS_Rev = rev,
                    //    TextTime = time,
                    //    Artificial="" };
                    //try
                    //{
                    //    var count = Mesdb.db.Queryable<ZhiKongInfo>().Where(it=>(it.SN==sn&&it.Code==code)||(it.BT_NAME==btname&&it.Code==code)).ToList();
                    //    if (count.Count==0)
                    //    {
                    //        Mesdb.db.Insertable(one).ExecuteCommand();
                    //    }
                    //    else
                    //    {
                    //        if (count.Count!=1)
                    //        {
                    //            result.success = false;
                    //            result.result = result.result+$"数据库重复,条码SN{sn}数据库SN{count[0].SN},蓝牙{btname}数据库蓝牙{count[0].BT_NAME}，数量{count.Count}";
                    //            break;
                    //        }
                    //        else
                    //        {
                    //            if (count[0].State=="通过")
                    //            {
                    //                result.success = false;
                    //                result.result = result.result+$"数据库重复,条码SN{sn}数据库SN{count[0].SN},蓝牙{btname}数据库蓝牙{count[0].BT_NAME}，状态通过";
                    //                break;
                    //            }
                    //        }
                    //        //result.success = false;
                    //        //result.result = $"数据库重复,条码SN{sn}数据库SN{count[0].SN},蓝牙{btname}数据库蓝牙{count[0].BT_NAME}";
                    //        //break;
                    //    }
                    //}
                    //catch (Exception ee)
                    //{
                    //    result.success = false;
                    //    result.result = result.result+"数据库上传失败" + 测试项序号 + 函数编号+ee.ToString();
                    //    break;
                    //}
                    var r7 = device.ReadSensor1(setcmd, setops, btnumber, setreserve1, setreserve2, setreserve3, 通道);
                    //初始化值
                    result.success = r7.IsSucceed;
                    if (!result.success)
                    {
                        result.result = result.result+r7.Err;
                        break;
                    }
                    string res7 = $"cmd:{r7.Value[0]},ops:{r7.Value[1]},date:{r7.Value[2]}";


                    if (r7.Value[0]!=getcmd.ToString())
                    {
                        result.success=false;
                        result.result =result.result+ res7+"cmd异常";
                        break;
                    }
                    if (r7.Value[1]!=getops.ToString())
                    {
                        result.success=false;
                        result.result =result.result+ res7+"ops异常";
                        break;
                    }
                    if (r7.Value[2]!=getdata.ToString())
                    {
                        result.success=false;
                        result.result =result.result+ res7+"data异常";
                        break;
                    }
                    break;
                case ZhiKongEnum.TestMode.初次测试检查:
                    try
                    {
                        if (testOrder.TestOrders.Count >= 测试项序号)
                        {
                            //获取对应测试项测试结果
                            var Parameter = testOrder.TestOrders[测试项序号 - 1]?.orders.FirstOrDefault(it => it.no == 函数编号.ToString());
                            if (Parameter!=null)
                            {
                                sntext = Parameter.result.result;
                            }
                        }
                        else
                        {
                            result.success = false;
                            result.result = "缺失取值数据" + 测试项序号 + 函数编号;
                            break;
                        }
                        if (!sntext.Contains(";"))
                        {
                            result.success = false;
                            result.result = "标签异常" + sntext;
                            break;
                        }
                        result.result = sntext+";";
                        string[] parts1 = sntext.Split(';');
                        if (parts1.Length!=3)
                        {
                            result.success = false;
                            result.result = "标签异常" + sntext;
                            break;
                        }
                        sn = parts1[0];
                        rev = parts1[1];
                        btname = parts1[2];
                        code = sn.Substring(0, 14);
                        number = btname.Substring(11);

                        btnumber = ulong.Parse(number);
                        if (sn.Length!=18)
                        {
                            result.success = false;
                            result.result = "编码长度错误" + sn;
                            break;
                        }
                    }
                    catch (Exception ee) {
                        result.success = false;
                        result.result = result.result+$"标签异常";
                        break;
                    }

                    //var time1 = DateTime.Now;
                    //ulong btnumber1;
                    //string sntext1 = "";
                    //string sn1 = "";
                    //string code1 = "";
                    //string btmac1 = "";
                    //string btname1 = "";
                    //string rev1 = "";
                    //string number1 = "";
                    ZhiKongInfo one1 = new ZhiKongInfo()
                    {

                        SN = sn,
                        Code = code,
                        BT_NAME = btname,
                        TestOS_Rev = rev,
                        Time = time,
                    };
                    try
                    {
                        var count = Mesdb.db.Queryable<ZhiKongInfo>().Where(it => (it.SN==sn&&it.Code==code)||(it.BT_NAME==btname&&it.Code==code)).ToList();
                        var count1 = Mesdb.db.Queryable<ZhiKongInfo>().Where(it => it.SN==sn&&it.Code==code&&it.BT_NAME==btname).ToList();
                       
                        if (count.Count==0)
                        {
                            Mesdb.db.Insertable(one1).ExecuteCommand();
                            result.success = true;
                            result.result = result.result+ $"数据库插入";
                            break;
                        }
                        else
                        {
                            if (count1.Count!=1)
                            {
                                result.success = false;
                                result.result = result.result+$"数据库单向匹配，标签错误";
                                break;
                            }
                            if (count.Count!=1)
                            {
                                result.success = false;
                                result.result = result.result+$"数据库多条";
                                break;
                            }
                            else
                            {
                                if (count[0].State=="通过")
                                {
                                    result.success = false;
                                    result.result = result.result+$"数据库一条状态通过";
                                    break;
                                }
                            }
                            //result.success = false;
                            //result.result = $"数据库重复,条码SN{sn}数据库SN{count[0].SN},蓝牙{btname}数据库蓝牙{count[0].BT_NAME}";
                            //break;
                        }
                        result.success = true;
                        result.result =result.result + $"数据库一条{count[0].State}";
                        break;
                    }
                    catch (Exception ee)
                    {
                        result.success = false;
                        result.result = result.result+"数据库失败";
                        break;
                    }
                    break;
                case ZhiKongEnum.TestMode.复测检查:
                    try
                    {
                        if (testOrder.TestOrders.Count >= 测试项序号)
                        {
                            //获取对应测试项测试结果
                            var Parameter = testOrder.TestOrders[测试项序号 - 1]?.orders.FirstOrDefault(it => it.no == 函数编号.ToString());
                            if (Parameter!=null)
                            {
                                sntext = Parameter.result.result;
                            }
                        }
                        else
                        {
                            result.success = false;
                            result.result = "缺失取值数据" + 测试项序号 + 函数编号;
                            break;
                        }
                        if (!sntext.Contains(";"))
                        {
                            result.success = false;
                            result.result = "标签异常" + sntext;
                            break;
                        }
                        result.result = sntext+";";
                        string[] parts2 = sntext.Split(';');
                        if (parts2.Length!=5)
                        {
                            result.success = false;
                            result.result = "标签异常" + sntext;
                            break;
                        }
                        sn = parts2[4];
                        rev = parts2[3];
                        btname = parts2[1];
                        code = sn.Substring(0, 14);
                        number = btname.Substring(11);

                        btnumber = ulong.Parse(number);
                    }
                    catch (Exception ee)
                    {
                        result.success = false;
                        result.result = result.result+$"标签异常";
                        break;
                    }
                    //var time1 = DateTime.Now;
                    //ulong btnumber1;
                    //string sntext1 = "";
                    //string sn1 = "";
                    //string code1 = "";
                    //string btmac1 = "";
                    //string btname1 = "";
                    //string rev1 = "";
                    //string number1 = "";
                    ZhiKongInfo one2 = new ZhiKongInfo()
                    {

                        SN = sn,
                        Code = code,
                        BT_NAME = btname,
                        TestOS_Rev = rev,
                        Time = time,
                    };
                    try
                    {
                        var count = Mesdb.db.Queryable<ZhiKongInfo>().Where(it => (it.SN==sn&&it.Code==code&&it.State=="通过")||(it.BT_NAME==btname&&it.Code==code&&it.State=="通过")).ToList();
                        var count1 = Mesdb.db.Queryable<ZhiKongInfo>().Where(it => it.SN==sn&&it.Code==code&&it.BT_NAME==btname).ToList();

                        if (count.Count==0)
                        {
                            result.success = false;
                            result.result = result.result+$"数据库没有，未过初始测试";
                            break;
                        }
                        if (count.Count!=1)
                        {
                            result.success = false;
                            result.result = result.result+$"数据库多条";
                            break;
                        }
                        else
                        {
                            if (count1.Count!=1)
                            {
                                result.success = false;
                                result.result = result.result+$"数据库单向匹配，标签错误";
                                break;
                            }
                            if (count[0].State1=="通过")
                            {
                                result.success = false;
                                result.result = result.result+$"数据库重复状态通过";
                                break;
                            }
                        }

                        result.success = true;
                        result.result =result.result + $"数据库一条{count[0].State1}";
                        break;

                    }
                    catch (Exception ee)
                    {
                        result.success = false;
                        result.result = result.result+"数据库上传失败" ;
                        break;
                    }
                    break;
                case ZhiKongEnum.TestMode.初次通过上传:

                        result.success = true;
                        result.result = "初次上传";
                    break;
                case ZhiKongEnum.TestMode.复测上传:
                    result.success = true;
                    result.result = "复测上传";
                    break;
                case ZhiKongEnum.TestMode.模式切换确认:
                    //初始化
                    var r11 = device.ReadSensor6(setcmd, setops, setdata, setreserve1, setreserve2, setreserve3, endtime);
                    //初始化值
                    result.success = r11.IsSucceed;
                    if (!result.success)
                    {
                        result.result = r11.Err;
                        break;
                    }
                    result.result = "OK";
                    break;
                default:
                    break;
            }
            return result;
        }
    }
}
